library(ggplot2)
library(dplyr)
library(readr)
library(qs)
dataGraph <- qread("data/dataGraph.qs")
dfgraph <- dataGraph$dfGraph_long
counterfactualM <- dataGraph$counterfactual_long
out <- dataGraph$out
dfgraph <- rename(dfgraph, "y" = y1_)
plot_vitd_masked <- function(
dfgraph,
counterfactualM,
mask_from = NULL,
mask_counterfactual_from = NULL,
x = 0.8
) {
# Filter data based on mask_from parameter instead of using rectangles
dfgraph_filtered <- if (!is.null(mask_from)) {
subset(dfgraph, time <= mask_from)
} else {
dfgraph
}
# Create counterfactual dataframe
counterfactual_df <- data.frame(
time = dfgraph$time,
counterfactual = counterfactualM
)
# Filter counterfactual data if needed
if (!is.null(mask_counterfactual_from)) {
counterfactual_df <- subset(counterfactual_df, time <= mask_counterfactual_from)
}
# Filter counterfactual by mask_from as well
if (!is.null(mask_from)) {
counterfactual_df <- subset(counterfactual_df, time <= mask_from)
}
p <- ggplot(dfgraph, aes(y = y * 100, x = time)) +
geom_line(
data = dfgraph_filtered,
aes(
color = "Observed",
size = "Observed",
linetype = "Observed"
)
) +
geom_line(
data = counterfactual_df,
aes(
y = counterfactual * 100,
color = "Counterfactual",
size = "Counterfactual",
linetype = "Counterfactual"
)
) +
geom_line(
data = subset(dfgraph_filtered, time <= 0),
aes(
y = pred * 100,
color = "Predicted",
size = "Predicted",
linetype = "Predicted"
)
) +
geom_line(
data = subset(dfgraph_filtered, time >= 0),
aes(
y = pred * 100,
color = "Predicted",
size = "Predicted",
linetype = "Predicted"
)
) +
scale_linetype_manual(
name = "",
values = c("Observed" = 1, "Predicted" = 1, "Counterfactual" = 4)
) +
scale_size_manual(
name = "",
values = c(
"Observed" = x * 0.8,
"Predicted" = x * 1,
"Counterfactual" = x * 1.25
)
) +
scale_color_manual(
name = "",
values = c(
"Observed" = "grey40",
"Predicted" = "#E22920",
"Counterfactual" = "#01BBA8"
)
) +
scale_x_continuous(
limits = c(-50, 18),
breaks = c(-50, -39, -27, -15, -3, 0, 9, 15, 21) - 15,
labels = c(
"Jan.\n2017",
"Jan.\n2018",
"Jan.\n2019",
"Jan.\n2020",
"Jan.\n2021",
"Apr.\n2021",
"Jan.\n2022",
"Jun.\n2022",
"Jan.\n2023"
)
) +
scale_y_continuous(
limits = c(1.5, 7),
breaks = seq(2, 7, 1)
) +
annotate(
"rect",
xmin = -28,
xmax = -24,
ymin = -Inf,
ymax = Inf,
alpha = 0.1,
fill = "#00857C"
) +
geom_text(
aes(x = -26, y = 1.8, label = "COVID19 \nLockdown"),
color = "#00857C",
size = x * 3
) +
labs(
y = "Monthly Vitamin D test prescriptions per 100 consultations",
x = NULL
) +
theme_classic() +
theme(
plot.background = element_rect(fill = "#F5F5F5", color = NA),
panel.background = element_rect(fill = "#F5F5F5", color = NA),
legend.background = element_rect(fill = "#EEEEEE"),
legend.position = "bottom",
panel.grid.major.x = element_line(color = "white"),
panel.grid.major.y = element_line(color = "white"),
axis.text.x = element_text(size = x * 10),
axis.text.y = element_text(size = x * 10),
axis.title = element_text(size = x * 11),
legend.text = element_text(size = x * 10)
)
# Add vertical lines conditionally based on data support
# Only add vline if the x-intercept is within the data range (mask_from)
if (is.null(mask_from) || mask_from >= -15) {
p <- p + geom_vline(
xintercept = -15,
color = "#E22920",
lty = 20,
linewidth = x * 1.5
)
}
if (is.null(mask_from) || mask_from >= 0) {
p <- p + geom_vline(
xintercept = 0,
color = "#675482",
lty = 20,
linewidth = x * 1.5
)
}
p
}